using UnityEngine;

public sealed class LerpHelper {
	public float TargetValue;
	/// <summary>
	/// Units per sec
	/// </summary>
	public float Speed;

	public float CurrentValue { get; private set; }

	public LerpHelper() { }

	public LerpHelper(float changeSpeed) {
		Speed = changeSpeed;
	}

	public LerpHelper(float changeSpeed, float initValue) {
		Speed = changeSpeed;
		CurrentValue = initValue;
		TargetValue = initValue;
	}

	public void SetImmediate(float value) {
		CurrentValue = value;
		TargetValue = value;
	}

	public void Update(float timeDelta) {
		var delta = TargetValue - CurrentValue;
		if ( delta > 0 ) {
			CurrentValue += Speed * timeDelta;
			CurrentValue = Mathf.Clamp(CurrentValue, -Mathf.Infinity, TargetValue);
		} else {
			CurrentValue -= Speed * timeDelta;
			CurrentValue = Mathf.Clamp(CurrentValue, TargetValue, Mathf.Infinity);
		}
	}
}
